//
//  ImageViewCell.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/18.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "ImageViewCell.h"
#import "ImgViewCell.h"
@interface ImageViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *imageCollectionView;
@property (nonatomic, strong) NSMutableArray *buttonSelectStatus;
@end

@implementation ImageViewCell


- (NSMutableArray *)buttonSelectStatus {
    if (!_buttonSelectStatus) {
        _buttonSelectStatus = [[NSMutableArray alloc] init];
    }
    return _buttonSelectStatus;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    
        UILabel *leftTitleLabel = [[UILabel alloc] init];
        leftTitleLabel.text = @"选择图片";
        leftTitleLabel.font = Font(30);
        leftTitleLabel.textColor = Color333333;
        [self.contentView addSubview:leftTitleLabel];
        [leftTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.top.mas_equalTo(Margin);
        }];
        
//        UILabel *rightLabel = [[UILabel alloc] init];
//        rightLabel.text = @"已选1张图片";
//        rightLabel.font = Font(30);
//        rightLabel.textColor = Color999999;
//        [self.contentView addSubview:rightLabel];
//        [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(-Margin);
//            make.top.mas_equalTo(Margin);
//        }];
        
        [self.contentView addSubview:self.imageCollectionView];
        [self.imageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.right.mas_equalTo(-Margin);
            make.top.equalTo(leftTitleLabel.mas_bottom).offset(Margin);
            make.height.mas_equalTo(jisuan(186));
        }];
    }
    return self;
}

- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
    [self.buttonSelectStatus removeAllObjects];
    for (int i = 0 ; i < imageArray.count; i++) {
        if (i==0) {
            [self.buttonSelectStatus addObject:@"1"];
        } else {
            [self.buttonSelectStatus addObject:@"0"];
        }
    }
    [self.imageCollectionView reloadData];
}

- (UICollectionView *)imageCollectionView {
    if (!_imageCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(jisuan(186), jisuan(186));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = Margin;
        _imageCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _imageCollectionView.backgroundColor = [UIColor whiteColor];
        _imageCollectionView.dataSource = self;
        _imageCollectionView.delegate = self;
        _imageCollectionView.alwaysBounceHorizontal = YES;
        _imageCollectionView.showsHorizontalScrollIndicator = NO;
        [_imageCollectionView registerClass:[ImgViewCell class] forCellWithReuseIdentifier:@"ImgViewCell"];
    }
    return _imageCollectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectClickBlock) {
        self.didSelectClickBlock(indexPath.item);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ImgViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImgViewCell" forIndexPath:indexPath];
    [cell setImageUrl:self.imageArray[indexPath.row] WithIndePathRow:indexPath.row ButtonSelectStatus:self.buttonSelectStatus[indexPath.row]];
    WS(weakSelf);
    cell.buttonSelectClickBlock = ^(NSInteger index) {
        [weakSelf contactImage:index];
    };
    return cell;
}

- (void)contactImage:(NSInteger)index {
    if (self.buttonHSelectClickBlock) {
        self.buttonHSelectClickBlock(index);
    }
    if ([self.buttonSelectStatus[index] isEqualToString:@"1"]) {
        return;
    }
    for (int i = 0; i < self.buttonSelectStatus.count; i ++) {
        if (index == i) {
            [self.buttonSelectStatus replaceObjectAtIndex:index withObject:@"1"];
        } else {
            [self.buttonSelectStatus replaceObjectAtIndex:i withObject:@"0"];
        }
    }
    [self.imageCollectionView reloadData];
}
@end
